<?php
namespace Orm\Product;

use Base\Exception\RmException;
class ProductClass extends \Base\BaseOrmObject{
    protected   $id,
				$name = '', // 商品名称
				$total_num = 0, // 奖品数量
                $residue_num = '',//剩余数量
				$description = '', // 商品描述
				$type = 0, // 1H5互动小游戏初级奖池   2H5互动小游戏中级奖池  3H5互动小游戏高级奖池
				$portrait = '', // 商品图片
				$is_del = 0, // 删除状态 0-正常 1-删除
				$insert_time = '', // 插入时间
				$update_time = ''; // 更新时间

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\Product\ProductModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'name' => $this->name,
			'total_num' => $this->total_num,
			'residue_num'=>$this->residue_num,
			'description' => $this->description,
			'type' => $this->type,
			'portrait' => $this->portrait,
        );

        $model = new \Orm\Product\ProductModel();
        $model->insert($data);
        $this->id = $model->getLastId();
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'name' => $this->name,
			'total_num' => $this->total_num,
            'residue_num'=>$this->residue_num,
			'description' => $this->description,
			'type' => $this->type,
			'portrait' => $this->portrait,
        );

        $model = new \Orm\Product\ProductModel();
        $model->update($data, "`id` = {$this->id}");
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\Product\ProductModel();
        $model->update($data, "`id` = {$id}");
    }

    private function checkNecessaryParam()
    {
    }

    /**
     * @return string
     */
    public static function getType($type)
    {
        $type_name = '';
        switch ($type){
            case '1':$type_name = 'H5互动小游戏初级奖池';break;
            case '2':$type_name = 'H5互动小游戏中级奖池';break;
            case '3':$type_name = 'H5互动小游戏高级奖池';break;
        }
        return $type_name;
    }
}